package com.ztesoft.sca.dao.user;

import com.ztesoft.sca.model.user.ImPrivilegePo;
import com.ztesoft.sca.model.user.ImRolePo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liang.aozhao
 * @created 2018/5/16 9:43
 */
@Repository
public class ImRoleDaoImpl implements ImRoleDao {

    private static final Logger logger = LoggerFactory.getLogger(ImRoleDaoImpl.class);
    @Autowired
    JdbcTemplate jdbcTemplate;

    @Override
    public long addImRole(ImRolePo imRolePo) throws Exception {
        String sql = "INSERT INTO IM_ROLE(ROLE_ID,ROLE_CODE,ROLE_NAME,COMMENTS) VALUES (?,?,?,?)";
        this.jdbcTemplate.update(sql, new Object[]{imRolePo.getRoleId(), imRolePo.getRoleCode(), imRolePo.getRoleName(), imRolePo.getComments()});
        return imRolePo.getRoleId();
    }

    @Override
    public void deleteImRole(ImRolePo imRolePo) throws Exception {
        //可按照角色id 或者 角色编码 删除
        StringBuffer sb = new StringBuffer();
        if (imRolePo.getRoleId() != null) {
            sb.replace(0, sb.length(), "DELETE FROM IM_ROLE WHERE ROLE_ID = ?");
            this.jdbcTemplate.update(sb.toString(), imRolePo.getRoleId());
        } else {
            sb.replace(0, sb.length(), "DELETE FROM IM_ROLE WHERE ROLE_CODE = ?");
            this.jdbcTemplate.update(sb.toString(), imRolePo.getRoleCode());
        }
    }

    @Override
    public void updateImRole(ImRolePo imRolePo) throws Exception {
        StringBuffer sql = new StringBuffer("UPDATE IM_ROLE SET ");
        List<Object> objects = new ArrayList<>();
        if (imRolePo.getRoleCode() != null && !"".equals(imRolePo.getRoleCode())) {
            sql.append(" ROLE_CODE = ? ,");
            objects.add(imRolePo.getRoleCode());
        }
        if (imRolePo.getRoleName() != null && !"".equals(imRolePo.getRoleName())) {
            sql.append(" ROLE_NAME = ? ,");
            objects.add(imRolePo.getRoleName());
        }
        if (imRolePo.getComments() != null && !"".equals(imRolePo.getComments())) {
            sql.append(" COMMENTS = ? ,");
            objects.add(imRolePo.getComments());
        }
        sql.deleteCharAt(sql.length() - 1);
        sql.append(" WHERE ROLE_ID = ? ");
        if(logger.isDebugEnabled()){
            logger.debug("执行的sql语句：" + sql.toString());
        }
        objects.add(imRolePo.getRoleId());
        Object[] args = objects.toArray();
        this.jdbcTemplate.update(sql.toString(), args);
    }

    @Override
    public ImRolePo getImRole(ImRolePo imRolePo) throws Exception {
        ImRolePo roleResult;
        String sql = "SELECT ROLE_ID,ROLE_CODE,ROLE_NAME,COMMENTS FROM IM_ROLE WHERE ROLE_ID = ?";
        roleResult = this.jdbcTemplate.queryForObject(sql, BeanPropertyRowMapper.newInstance(ImRolePo.class),imRolePo.getRoleId());
        return roleResult;
    }

    @Override
    public List<ImRolePo> listAllImRoles() throws Exception {
        String sql = "SELECT ROLE_ID,ROLE_CODE,ROLE_NAME,COMMENTS FROM IM_ROLE ";
        List<ImRolePo> imRolePoList = this.jdbcTemplate.query(sql, BeanPropertyRowMapper.newInstance(ImRolePo.class));
        return imRolePoList;
    }

    @Override
    public List<ImRolePo> listImRoleByUserId(Long userId) throws Exception {
        String sql = "SELECT A.ROLE_ID,A.ROLE_CODE,A.ROLE_NAME,A.COMMENTS FROM IM_ROLE A , IM_ROLE_USER_RELA B WHERE B.USER_ID = ?  AND A.ROLE_ID = B.ROLE_ID ";
        List<ImRolePo> imRolePoList = this.jdbcTemplate.query(sql, new Object[]{userId}, BeanPropertyRowMapper.newInstance(ImRolePo.class));
        return imRolePoList;
    }

    @Override
    public List<ImRolePo> listImRoleByOrgId(Long orgId) throws Exception {
        String sql = "SELECT A.ROLE_ID,A.ROLE_CODE,A.ROLE_NAME,A.COMMENTS FROM IM_ROLE A , IM_ORG_ROLE_RELA B WHERE B.ORG_ID = ?  AND A.ROLE_ID = B.ROLE_ID ";
        List<ImRolePo> imRolePoList = this.jdbcTemplate.query(sql, new Object[]{orgId}, BeanPropertyRowMapper.newInstance(ImRolePo.class));
        return imRolePoList;
    }

    @Override
    public void addImRoleOrg(Long roleId, Long orgId) throws Exception {
        String sql = "INSERT INTO IM_ORG_ROLE_RELA(ROLE_ID,ORG_ID) VALUES (?,?) ";
        this.jdbcTemplate.update(sql, new Object[]{roleId, orgId});
    }

    @Override
    public void deleteImRoleOrg(Long roleId, Long orgId) throws Exception {
        String sql = "DELETE FROM IM_ORG_ROLE_RELA WHERE ROLE_ID = ? AND ORG_ID = ?";
        this.jdbcTemplate.update(sql, new Object[]{roleId, orgId});
    }

    @Override
    public List<ImPrivilegePo> listImPrivilegeByRoleId(Long roleId) throws Exception {
        String sql = "SELECT PRIVILEGE_ID , BUTTON_ID ,MENU_ID ,ROLE_ID , ACCESS_TYPE FROM IM_PRIVILEGE WHERE ROLE_ID = ? ";
        List<ImPrivilegePo> imPrivilegePoList = this.jdbcTemplate.query(sql,new Object[]{roleId},BeanPropertyRowMapper.newInstance(ImPrivilegePo.class));
        return imPrivilegePoList;
    }

}
